package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.TradeRecordingEntity;
import io.renren.modules.app.entity.vo.QueryRechargeParam;
import io.renren.modules.app.entity.vo.SelectRechargeData;
import io.renren.modules.app.service.TradeRecordingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.RechargeEntity;
import io.renren.modules.app.service.RechargeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 充值记录表
 *
 * @date 2022-04-20 22:37:44
 */
@RestController
@RequestMapping("app/recharge")
public class RechargeController {
    @Autowired
    private RechargeService rechargeService;

    @Autowired
    private TradeRecordingService tradeRecordingService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:recharge:list")
    public R list(QueryRechargeParam rechargeData) {
        SelectRechargeData selectRechargeData = rechargeService.queryWithdrawalsRecording(rechargeData);
        return R.ok().put("page", selectRechargeData);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rechargeId}")
    @RequiresPermissions("generator:recharge:info")
    public R info(@PathVariable("rechargeId") Long rechargeId) {
        RechargeEntity recharge = rechargeService.getById(rechargeId);

        return R.ok().put("recharge", recharge);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:recharge:save")
    public R save(@RequestBody RechargeEntity recharge) {
        rechargeService.save(recharge);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:recharge:update")
    public R update(@RequestBody RechargeEntity recharge) {
        rechargeService.updateById(recharge);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:recharge:delete")
    public R delete(@RequestBody Long[] rechargeIds) {
        rechargeService.removeByIds(Arrays.asList(rechargeIds));

        return R.ok();
    }


    /**
     * 分页获取充值记录
     */
    @Login
    @GetMapping("/queryRechargeList")
    public R queryRechargeList(QueryRechargeParam recharge) {
        SelectRechargeData selectRechargeData = rechargeService.queryWithdrawalsRecording(recharge);
        return R.ok().put("rechargeData", selectRechargeData);
    }

    /**
     * 新增充值信息
     *
     * @param recharge 充值信息类
     * @return
     */
    @Login
    @PostMapping("/addRechargeData")
    public R addRechargeData(@RequestBody RechargeEntity recharge) {
        Integer integer = rechargeService.addRechargeData(recharge);
        if (integer > 0) {
            TradeRecordingEntity tradeRecording = new TradeRecordingEntity();
            tradeRecording.setUserId(recharge.getUserId());
            tradeRecording.setTradeAmount(recharge.getTradeAmount());
            tradeRecording.setTradeType(0);
            //新增交易记录
            tradeRecordingService.addTradeRecording(tradeRecording);
            return R.ok("充值成功");
        } else {
            return R.error("充值提交失败");
        }
    }


}
